package com.ruoyi.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.ams.domain.AmsAssetInventoryDetail;
import com.ruoyi.ams.domain.vo.AmsAssetInventoryDetailVo;
import com.ruoyi.ams.domain.vo.AssetFullInfoEditVo;
import com.ruoyi.ams.service.IAmsAssetInventoryDetailService;
import com.ruoyi.asms.controller.AsmsAssetFullInfoController;
import com.ruoyi.common.core.domain.AsmsAssetFullInfo;
import com.ruoyi.common.core.domain.KeyValueVo;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 盘点明细Controller
 * 
 * @author yfkj
 * @date 2022-05-16
 */
@RestController
@RequestMapping("/ams/inventoryDetail")
public class AmsAssetInventoryDetailController extends BaseController
{
    @Autowired
    private IAmsAssetInventoryDetailService amsAssetInventoryDetailService;

    @Autowired
    private AssetInfoViewController assetInfoViewController;

//    @Autowired
//    RemoteAsmsService remoteAsmsService;
    @Autowired
    private AsmsAssetFullInfoController asmsAssetFullInfoController;
    /**
     * 查询盘点明细列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:list")
    @ApiOperation(value = "获取盘点明细列表")
    @GetMapping("/getAmsAssetInventoryDetailList")
    public AjaxResult getAmsAssetInventoryDetailList(AmsAssetInventoryDetail amsAssetInventoryDetail)
    {
        startPage();
        List<AmsAssetInventoryDetail> detailList = amsAssetInventoryDetailService.selectAmsAssetInventoryDetailList(amsAssetInventoryDetail);

        List<Long> assetIds = new LinkedList<>();

        HashMap<Long, AmsAssetInventoryDetailVo> map = new HashMap<>();

        for(AmsAssetInventoryDetail detail :  detailList){
            assetIds.add(detail.getAssetId());
            AmsAssetInventoryDetailVo vo=new AmsAssetInventoryDetailVo();
            BeanUtils.copyProperties(detail, vo);
            map.put(detail.getAssetId(),vo);
        }

        List<AmsAssetInventoryDetailVo> list= new LinkedList<>();
        if(amsAssetInventoryDetail.getCode() != null && !amsAssetInventoryDetail.getCode().equals("")){
            AsmsAssetFullInfo asmsAssetFullInfo = asmsAssetFullInfoController.getRAssetByScanCode(amsAssetInventoryDetail.getCode()).getData();

            if(asmsAssetFullInfo == null || map.get(asmsAssetFullInfo.getAssetId()) == null){
                return AjaxResult.success(list);
            }

            BeanUtils.copyProperties(asmsAssetFullInfo,map.get(asmsAssetFullInfo.getAssetId()));

            list.add(map.get(asmsAssetFullInfo.getAssetId()));

            return AjaxResult.success(list);
        }

        if(assetIds.size() < 1){
            return AjaxResult.success(list);
        }
        List<AsmsAssetFullInfo> fullInfos = asmsAssetFullInfoController.getFullInfoByAssetIds(assetIds).getData();

        if(fullInfos == null){
            return AjaxResult.error("remoteAsmsService调用失败");
        }

        for(AsmsAssetFullInfo fullInfo : fullInfos){
            if(map.get(fullInfo.getAssetId()) == null){
               continue;
            }
            BeanUtils.copyProperties(fullInfo,map.get(fullInfo.getAssetId()));
        }

        for(Long assetId : map.keySet()){
            list.add(map.get(assetId));
        }
        return AjaxResult.success(list);
    }


    /**
     * 获取盘点明细详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:query")
    @ApiOperation(value = "获取盘点明细详情")
    @GetMapping(value = "/getAmsAssetInventoryDetailById")
    public AjaxResult getAmsAssetInventoryDetailById(@RequestParam("inventoryDetailId") Long inventoryDetailId)
    {
        AmsAssetInventoryDetail detail = amsAssetInventoryDetailService
                .getOne(new QueryWrapper<AmsAssetInventoryDetail>()
                        .eq(AmsAssetInventoryDetail.INVENTORY_ID, inventoryDetailId), false);

        AmsAssetInventoryDetailVo vo = new AmsAssetInventoryDetailVo();
        BeanUtils.copyProperties(detail,vo);

        List<Long> assetIds = new LinkedList<>();
        assetIds.add(detail.getAssetId());

        List<AsmsAssetFullInfo> fullInfos = asmsAssetFullInfoController.getFullInfoByAssetIds(assetIds).getData();
        if(fullInfos == null){
            return AjaxResult.error("asms服务调用失败");
        }

        BeanUtils.copyProperties(fullInfos.get(0),vo);

        return AjaxResult.success(vo);
    }

    /**
     * 新增盘点明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:add")
    @Log(title = "盘点明细", businessType = BusinessType.INSERT)
    //@ApiOperation(value = "新增盘点明细")
    @PostMapping(value = "/addAmsAssetInventoryDetail")
    public AjaxResult addAmsAssetInventoryDetail(@RequestBody AmsAssetInventoryDetail amsAssetInventoryDetail)
    {
        return toAjax(amsAssetInventoryDetailService.insertAmsAssetInventoryDetail(amsAssetInventoryDetail));
    }

    /**
     * 修改盘点明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:edit")
    @Log(title = "盘点明细", businessType = BusinessType.UPDATE)
    //@ApiOperation(value = "修改盘点明细")
    @PostMapping(value = "/editAmsAssetInventoryDetail")
    public AjaxResult editAmsAssetInventoryDetail(@RequestBody AmsAssetInventoryDetail amsAssetInventoryDetail)
    {
        AssetFullInfoEditVo vo = new AssetFullInfoEditVo();
        BeanUtils.copyProperties(amsAssetInventoryDetail,vo);
        vo.setAssetId(amsAssetInventoryDetail.getNewAddressId());
        vo.setUserId(amsAssetInventoryDetail.getNewUserId());
        vo.setUseDeptId(amsAssetInventoryDetail.getNewUseDeptId());
        vo.setAssetId(amsAssetInventoryDetail.getAssetId());
        assetInfoViewController.editFullAssetInfo(vo);

        return toAjax(amsAssetInventoryDetailService.updateAmsAssetInventoryDetail(amsAssetInventoryDetail));
    }

    /**
     * 删除盘点明细
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:remove")
    @Log(title = "盘点明细", businessType = BusinessType.DELETE)
	@GetMapping("/removeAmsAssetInventoryDetails")
    public AjaxResult removeAmsAssetInventoryDetails(@RequestParam Long[] inventoryDetailIds)
    {
        return toAjax(amsAssetInventoryDetailService.deleteAmsAssetInventoryDetailByInventoryDetailIds(inventoryDetailIds));
    }
    @GetMapping("/getAmsAssetInventoryDetailNamesForSelect")
    //@ApiOperation(value = "获取盘点明细名称列表用于下拉搜索")
    public AjaxResult getAmsAssetInventoryDetailNamesForSelect()
    {
        List<AmsAssetInventoryDetail> list = amsAssetInventoryDetailService.list();//自行加条件
        List<KeyValueVo> resultList = new ArrayList<>();
        for (AmsAssetInventoryDetail amsAssetInventoryDetail: list ) {
            KeyValueVo vo = new KeyValueVo();
            //vo.setKey(it.getId());
            //vo.setValue(it.getName());
            resultList.add(vo);
        }
        return AjaxResult.success(resultList);
    }
    /**
     * 导出盘点明细列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:detail:export")
    @Log(title = "盘点明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAmsAssetInventoryDetail")
    public String exportAmsAssetInventoryDetail(HttpServletResponse response, AmsAssetInventoryDetail amsAssetInventoryDetail) throws IOException
    {
        List<AmsAssetInventoryDetail> list = amsAssetInventoryDetailService.selectAmsAssetInventoryDetailList(amsAssetInventoryDetail);
        ExcelUtil<AmsAssetInventoryDetail> util = new ExcelUtil<AmsAssetInventoryDetail>(AmsAssetInventoryDetail.class);
        util.exportExcel(response, list, "盘点明细数据");
        return "";
    }

}
